package cn.hsw.platform.core.configure;

import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor;

import javax.servlet.ServletRequest;


/**
 * 自定义属性处理器
 *      新增参数绑定器用
 * @author 李子木
 * @date 2020-11-02
 */
public class CustomServletModelAttributeMethodProcessor extends ServletModelAttributeMethodProcessor {
    public CustomServletModelAttributeMethodProcessor(boolean annotationNotRequired) {
        super(annotationNotRequired);
    }

    @Override
    protected void bindRequestParameters(WebDataBinder webDataBinder, NativeWebRequest request) {
        ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
        Assert.state(servletRequest != null,"No servletRequest");
        ServletRequestDataBinder servletRequestDataBinder = (ServletRequestDataBinder) webDataBinder;
        //更换请求参数绑定器
        new CustomServletRequestDataBinder(servletRequestDataBinder.getTarget()).bind(servletRequest);
    }
}
